﻿using System.IO;
using System.Text;
using System.Windows;

using Aliphant.Common;
using Aliphant.Common.Managers;
using Aliphant.Interfaces;
using Aliphant.IoC;
using Aliphant.Mvvm.XEvent;
using Aliphant.Mvvm.XEvent.Ioc;
using Aliphant.Script;
using Aliphant.UI.Browser;
using Aliphant.UI.Forms.Notify;
using Aliphant.UI.Services;
using Aliphant.Windows;


namespace XDeskTool
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : XApplication
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            // 日志框架
            FileInfo fi = new FileInfo("log4net.config");
            log4net.Config.XmlConfigurator.ConfigureAndWatch(fi);

            NotifyIconHelper.Init();

            var mainWindow = UIContainerLocator.Container.Resolve<MainWindow>("mainWin");
            Application.Current.MainWindow = mainWindow;
            Application.Current.MainWindow.Show();

            IoCServer.Container.Resolve<ThemeService>().ListeningSysTheme();

            Logger.Info("APP Start");
        }

        public override void RegisterService(IDiContainer diContainer)
        {
            diContainer.AddSingleton<ThemeService>(new ThemeService());
        }

        public override void RegisterModule(IUIContainer container, IDiContainer diContainer)
        {
            // 手动注册模块
            container.AddUIModule<BrowserModule>();
            container.AddUIModule<WindowModule>();
            diContainer.AddModule<ScriptModule>();
        }

        public override IModuleCatalog ConfigurationModule()
        {
            return new XModuleCatalog() { ModuleDir = "./Modules" };
        }

        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);

            if (Directory.Exists("caches"))
            {
                Directory.Delete("caches", true);
            }

            ConfigManager.Instance.Save();
        }
    }

}
